package com.example.pinkcat.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.PagerTabStrip;
import androidx.viewpager.widget.ViewPager;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.example.pinkcat.R;
import com.example.pinkcat.activity.adapter.U_viewpager_adapter;
import com.example.pinkcat.activity.api.getData.GetData;
import com.example.pinkcat.activity.pinkCatUser.userStaticMsg;
import com.example.pinkcat.api.entity.FanKeepStaticMsg;
import com.example.pinkcat.api.getData.getData_waibu;
import com.rishabhharit.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.Objects;

public class Userpage extends AppCompatActivity {

    Context context=this;
    ViewPager viewPager;//viewpager组件
    PagerTabStrip pagerTab;  //PagerTabStrip组件
    ArrayList<View> views=new ArrayList<>();

    //关注/粉丝
    TextView fansnum;
    TextView fanstxt;
    TextView followsnum;
    TextView followtxt;
    View followlayout;//跳转关注
    View fanslayout;//跳转粉丝

    RoundedImageView headimg;//头像
    TextView username;//名称
    TextView userid;//id
    TextView vip_ico;//vip样式

    TextView followbtn;

    boolean isCollectPage=false;

    String intent_up_id="-1";//用户

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }

    @Override
    protected void onResume() {
        super.onResume();
        //个人页面设置用户信息
        GetData.getuserpageuserinfo(intent_up_id,context,headimg,username,vip_ico,followbtn);
        userid.setText(intent_up_id);
        //    //个人页面设置用户关注/粉丝数量
        if (getData_waibu.getkeep(context,intent_up_id)){
            fansnum.setText(FanKeepStaticMsg.fan);
            followsnum.setText(FanKeepStaticMsg.keep);
            fansnum.setTextColor(getResources().getColor(R.color.pink));
            followsnum.setTextColor(getResources().getColor(R.color.pink));
        }
        if (userStaticMsg.vip==1){
            username.setTextColor(getResources().getColor(R.color.pink));
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Objects.requireNonNull(getSupportActionBar()).hide();//去掉标题栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        setContentView(R.layout.activity_userpage);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility( View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN|View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }//深色信息栏

        //获取id
        Intent intent=getIntent();
        if (intent!=null){
            intent_up_id=intent.getStringExtra("up_id");//接收用户id
        }






        viewPager=findViewById(R.id.user_viewpager);//viewpager
        pagerTab=findViewById(R.id.user_viewpager_Table);//viewpager 标题
        fansnum=findViewById(R.id.fans_num);
        fanstxt=findViewById(R.id.fans);
        followsnum=findViewById(R.id.follow_num);
        followtxt=findViewById(R.id.follow);
        headimg=findViewById(R.id.userpage_userhead);
        username=findViewById(R.id.username);
        userid=findViewById(R.id.user_id);
        vip_ico=findViewById(R.id.vip_target);
        followlayout=findViewById(R.id.follow_layout);
        fanslayout=findViewById(R.id.fans_layout);
        followbtn=findViewById(R.id.followbtn);
        if (userStaticMsg.userId.equals(intent_up_id)){//自己进入自己页面不需要关注按牛
            followbtn.setVisibility(View.INVISIBLE);
        }


        //监听事件
        listen();


        //初始化viewpager
        SetViews();
        U_viewpager_adapter video_viewpager_adapter=new U_viewpager_adapter(new String[]{"收藏", "动态"},views,context,intent_up_id);
        viewPager.setAdapter(video_viewpager_adapter);
        pagerTab.setTabIndicatorColor(getResources().getColor(R.color.pink));//标题栏指示器的颜色
        //pagerTab.setTextColor(getResources().getColor(R.color.pink));  //设置标题字体颜色
        pagerTab.setTextSpacing(20);
        pagerTab.setTextSize(1, 16);  //设置标题字体大小
        pagerTab.setDrawFullUnderline(false);//下横线
        viewPager.setCurrentItem(0);//设定默认选中的
        if (isCollectPage){
            viewPager.setCurrentItem(0);//设定默认选中的
        }
        else {
            viewPager.setCurrentItem(1);//设定默认选中的
        }
    }

    void listen(){
        //关注与取关
        followbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (followbtn.getText().toString().equals("+ 关 注")){
                    if (GetData.followOrUnfollow(context,intent_up_id,true)){
                        followbtn.setText(R.string.unfollow);
                        followbtn.setTextColor(context.getResources().getColor(R.color.black_overlay));
                        followbtn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_grey));
                    }
                }
                else {
                    if (GetData.followOrUnfollow(context,intent_up_id,false)){
                        followbtn.setText(R.string.follow);
                        followbtn.setTextColor(context.getResources().getColor(R.color.white));
                        followbtn.setBackground(context.getResources().getDrawable(R.drawable.backgroud_cycle_pink));
                    }
                }
            }
        });
        //粉丝关注
        followlayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EnterFansFollowPage(false);
            }
        });
        fanslayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EnterFansFollowPage(true);
            }
        });


        //返回监听
        findViewById(R.id.user_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    public void SetViews(){
        views.add(getLayoutInflater().inflate(R.layout.userpage_follow_send, null));
        views.add(getLayoutInflater().inflate(R.layout.userpage_follow_send, null));
    }

    //跳转到粉丝关注列表
    private void EnterFansFollowPage(boolean isFansPage){
        Intent intent =new Intent(context,FansFollowPage.class);
        intent.putExtra("isFansPage",isFansPage);
        intent.putExtra("up_id",intent_up_id);
        startActivity(intent);
    }
}